<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>parent_id 查询 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'query-dsl-parent-id-query.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-parent-id-query.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-parent-id-query.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/query-dsl-parent-id-query.html" rel="nofollow" target="_blank">../en/query-dsl-parent-id-query.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="query-dsl.html">查询领域特定语言(Query DSL)</a></span>
»
<span class="breadcrumb-link"><a href="joining-queries.html">联结查询</a></span>
»
<span class="breadcrumb-node">parent_id 查询</span>
</div>
<div class="navheader">
<span class="prev">
<a href="query-dsl-has-parent-query.html">« has_parent 查询</a>
</span>
<span class="next">
<a href="_notes_3.html">注意 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="query-dsl-parent-id-query"></a>parent_id 查询
</h2>
</div></div></div>

<p>
返回<a class="xref" href="parent-join.html" title="Join datatype">联结(joined)</a>到指定父文档的子文档。

可以使用 <a class="xref" href="parent-join.html" title="Join datatype">join</a> 字段映射在同一索引中的文档之间创建父子关系。
</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="parent-id-query-ex-request"></a>请求示例
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="parent-id-index-setup"></a>创建索引
</h4>
</div></div></div>
<p>
要使用 <code class="literal">parent_id</code> 查询，索引必须包含 <a class="xref" href="parent-join.html" title="Join datatype">join</a> 字段映射。

若要查看如何为 <code class="literal">parent_id</code> 查询创建索引，请尝试下面这个例子。
</p>
<div class="olist orderedlist">
<ol class="orderedlist">
<li class="listitem">
<p>创建一个使用了 <a class="xref" href="parent-join.html" title="Join datatype">join</a> 字段映射的索引：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /my-index
{
    "mappings": {
        "properties" : {
            "my-join-field" : {
                "type" : "join",
                "relations": {
                    "my-parent": "my-child"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/211.console"></div>
</li>
<li class="listitem">
<p>添加并索引一个ID为<code class="literal">1</code>的父文档：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /my-index/_doc/1?refresh
{
  "text": "This is a parent document.",
  "my-join-field": "my-parent"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/212.console"></div>
</li>
<li class="listitem">
<p>添加并索引一个该父文档的子文档，ID为<code class="literal">2</code>：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /my-index/_doc/2?routing=1&amp;refresh
{
  "text": "This is a child document.",
  "my_join_field": {
    "name": "my-child",
    "parent": "1"
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/213.console"></div>
</li>
</ol>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="parent-id-query-ex-query"></a>查询示例
</h4>
</div></div></div>
<p>下面这个搜索返回ID为<code class="literal">1</code>的父文档的子文档：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /my-index/_search
{
  "query": {
      "parent_id": {
          "type": "my-child",
          "id": "1"
      }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/214.console"></div>
</div>

</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="parent-id-top-level-params"></a><code class="literal">parent_id</code>的顶级参数
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">type</code>
</span>
</dt>
<dd>
(必需, string) 为 <a class="xref" href="parent-join.html" title="Join datatype">join</a> 字段映射的子关系的名称。
</dd>
<dt>
<span class="term">
<code class="literal">id</code>
</span>
</dt>
<dd>
(必需, string) 父文档的ID。查询会返回该父文档的子文档。
</dd>
<dt>
<span class="term">
<code class="literal">ignore_unmapped</code>
</span>
</dt>
<dd>
<p>
(可选, boolean) 指示是否忽略未映射的 <code class="literal">type</code> 并且不返回任何文档而不是错误。默认为<code class="literal">false</code>。
</p>
<p>
如果为 <code class="literal">false</code>，当 <code class="literal">type</code> 未映射时 Elasticsearch 会返回错误。
</p>
<p>
可以使用此参数来查询可能不包含 <code class="literal">type</code> 的多个索引。
</p>
</dd>
</dl>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="query-dsl-has-parent-query.html">« has_parent 查询</a>
</span>
<span class="next">
<a href="_notes_3.html">注意 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>